﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;



namespace STHPMS1.BLL.B_Bus
{
    public class Df_Funlim_ProjBus : BllBase, IDf_Funlim_ProjBill
    {
        public void Save<T>(T entity)
            where T : class ,IDf_Funlim_Proj
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDf_Funlim_Proj entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDf_Funlim_Proj
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info).OrderBy(q=>q.Beginmp);
        }


        /// <summary>
        /// 资金受限 资金分配
        /// </summary>
        /// <param name="anniname"></param>
        /// <param name="analy_year"></param>
        public void Insert(string anniname, string analy_year)
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Delete<Df_Funlim_Proj>(p => p.Anni_Name == anniname && p.Analy_Year == analy_year);  //资金模型名称

            Expression<Func<Df_Costmod_Para, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year;

            List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList();  //资金模型参数   理论上只有一条数据
            //int i = 0;
            foreach (Df_Costmod_Para info in data)
            {
                List<Dn_Divi_Planning> list = dao.Df_Main_Cost.GetDivi<Dn_Divi_Planning>(info.Anni_Name, info.Analy_Year);

                foreach (Dn_Divi_Planning diviinfo in list)
                {

                    Df_Funlim_Proj funinfo = new Df_Funlim_Proj();
                    funinfo.Id = serv.NewGuid();  //ID
                    funinfo.Anni_Name = anniname; //资金模型名称
                    funinfo.Roadnet_Name = info.Roadnet_Name; //路网名称
                    funinfo.Rdwayid = diviinfo.Rdwayid; //路线编号
                    funinfo.Mainsegid = diviinfo.Mainsegid; //决策路段
                    funinfo.Beginmp = diviinfo.Beginmp; //起点桩号
                    funinfo.Endmp = diviinfo.Endmp; //终点桩号
                    funinfo.Rd_Class = dao.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid).Rdclass_Dictid; //公路等级
                    funinfo.Segid_Main = diviinfo.Segid_Main;  //养护类型
                    funinfo.Main_Meas = diviinfo.Project_Name; //养护方案
                    funinfo.Carry_Year = info.Analy_Year; // 实施年度
                    funinfo.Segid_Maincost = diviinfo.Cost; //方案资金
                    funinfo.Analy_Year = info.Analy_Year;  //分析年度
                    funinfo.Pci = diviinfo.Pci; //PCI的值

                    

                    #region 原有代码  带删除
                    /*  
                    Random ro = new Random();
                    //funinfo.Pci = ro.Next(5, 100);  //??
                    //funinfo.Pqi = ro.Next(5, 100);  //？？

                    funinfo.Pci = dao.Df_Funlim_Proj.GetDn_Divi_Planning(anniname,info.Analy_Year, diviinfo.Mainsegid).Pci;
                    funinfo.Pqi = dao.Df_Funlim_Proj.GetDn_Divi_Planning(anniname,info.Analy_Year, diviinfo.Mainsegid).Pqi;


                    //该路段为大中修养护，当年养护后，以后每年则进行日常性养护
                    if ((i >= 1 && diviinfo.Segid_Main == 12002) || (i >= 1 && diviinfo.Segid_Main == 12003))
                    {
                        funinfo.Segid_Main = 12000;
                    }

                    if (diviinfo.Segid_Main == 12000) //该路段为日常养护
                    {
                        funinfo.Segid_Main = 12000;
                    }
                    if (diviinfo.Segid_Main == 12001)   //该路段为预防性养护，判断是否单年养护，如果不是则进行日常性养护
                    {
                        string mainyear = dao.Dp_Preven_Detail.GetMainYear(diviinfo.Mainsegid);
                        if (info.Analy_Year == mainyear)
                        {
                            funinfo.Segid_Main = 12001;
                        }
                        else
                        {
                            funinfo.Segid_Main = 12000;
                        }
                    }

                    if ((i == 0 && diviinfo.Segid_Main == 12002) || (i == 0 && diviinfo.Segid_Main == 12003))  //该路段为大中修
                    {
                        funinfo.Segid_Main = diviinfo.Segid_Main;
                    }


                    //计算费用

                    if (funinfo.Segid_Main == 12000)
                    {
                        double lenght = Convert.ToDouble(diviinfo.Endmp - diviinfo.Beginmp);

                        //通过决策路段获取基础路段信息
                        IB_Invntory b_invntoryInfo = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid);
                        int Year = (DateTime.Now - Convert.ToDateTime(b_invntoryInfo.Begindate)).Days / 365;

                        double cost = dao.Df_Dailymian_Cost.GetDaily_Unitcost(Convert.ToInt32(b_invntoryInfo.Pvmttype_Dictid), b_invntoryInfo.Rdclass_Dictid.ToString(), Year);  //通车年限？？？
                        funinfo.Segid_Maincost = lenght / 1000 * cost;
                    }
                    if (funinfo.Segid_Main == 12001)
                    {
                        funinfo.Segid_Maincost = dao.Dp_Preven_Detail.GetCost(diviinfo.Mainsegid);
                    }
                    if (funinfo.Segid_Main == 12002)
                    {
                        funinfo.Segid_Maincost = 10;   //??
                    }
                    if (funinfo.Segid_Main == 12003)
                    {
                        funinfo.Segid_Maincost = 100;  //??
                    }
                    */
                    #endregion 

                    dao.Save<Df_Funlim_Proj>(funinfo);
                }

                //i++;
            }
        }


        public void GetEditYear(string anniname, string analy_year)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            Expression<Func<Df_Costmod_Para, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year.ToString();

            List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList();  //资金模型参数 理论上只有一条数据

            foreach (Df_Costmod_Para info in data)
            {

                double annu_fundlim = Convert.ToDouble(info.Annu_Fundlim); //资金限额/万元

                Expression<Func<Df_Full_Order, bool>> exp1 = null;
                List<Df_Full_Order> data1 = this.Scene.Bll.Df_Full_Order.Get<Df_Full_Order>(exp1, null).OrderBy(p => p.Ordernum).ToList();  //各级公路资金分配表

                foreach (Df_Full_Order info1 in data1)
                {
                    GetS(dao, info.Analy_Year, anniname, Convert.ToInt32(info1.Rd_Class), Convert.ToInt32(info1.Max_Pci), ref annu_fundlim);
                }
            }
        }

        int i = 1;
        
        public void GetS(ISthpms1Dao dao, string year, string anniname, int rd_class, int pci, ref double annu_fundlim)
        {
            IDf_Funlim_Proj funinfo = dao.Df_Funlim_Proj.GetDf_Funlim_Proj(anniname,year, rd_class, pci);
            if (funinfo != null)
            {
                if (annu_fundlim >= funinfo.Segid_Maincost)  //资金限额大于等于路段养护费用
                {
                    dao.Update<Df_Funlim_Proj>(new { Ordernum = i, Cost_Year = year }, p => p.Id == funinfo.Id);

                    //更新到 路网决策路段表中
                    dao.Update<Dn_Divi_Planning>(new { MainCost = funinfo.Segid_Maincost }, p => p.Anni_Name == funinfo.Anni_Name && p.Divi_Time == funinfo.Analy_Year && p.Mainsegid == funinfo.Mainsegid);

                    annu_fundlim = annu_fundlim - Convert.ToDouble(funinfo.Segid_Maincost);
                    i++;
                    GetS(dao, year, anniname, rd_class, pci, ref annu_fundlim);
                }
            }
        }


        /// <summary>
        /// 获取资金受限条件养护方案表符合条件的一条数据
        /// </summary>
        /// <param name="anniname">资金模型名称</param>
        /// <param name="rd_class">技术等级</param>
        /// <param name="pci">PCI值</param>
        /// <param name="pqi">PQI值</param>
        /// <returns></returns>
        public IDf_Funlim_Proj GetDf_Funlim_Proj(string anniname, string Analy_Year, int rd_class, int pci)
        {
            return this.Scene.Dal.Sthpms1.Df_Funlim_Proj.GetDf_Funlim_Proj(anniname,Analy_Year, rd_class, pci);
        }

         /// <summary>
        /// 获取规划年度养护需求分析信息
        /// </summary>
        /// <param name="divi_time">规划年度</param>
        /// <param name="mainsegid">决策路段</param>
        /// <returns></returns>
        public IDn_Divi_Planning GetDn_Divi_Planning(string anniname,string divi_time, string mainsegid)
        {
            return this.Scene.Dal.Sthpms1.Df_Funlim_Proj.GetDn_Divi_Planning(anniname,divi_time, mainsegid);
        }
    }
}
